package com.example.loggingservice.exception;

import com.example.common.event.OperationLogEvent;
import com.example.loggingservice.entity.OperationLog;
import com.example.loggingservice.service.OperationLogService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
@Slf4j
public class OperationLogHandler {
    
    private final OperationLogService operationLogService;

    public void processOperationLog(OperationLogEvent event) {
        try {
            OperationLog logEntity = new OperationLog();
            logEntity.setUserId(event.getUserId());
            logEntity.setAction(event.getAction());
            logEntity.setIp(event.getIp());
            logEntity.setDetail(event.getDetail());
            operationLogService.save(logEntity);
            log.debug("操作日志已保存: {}", event);
        } catch (Exception e) {
            log.error("保存日志失败", e);
            throw new RuntimeException(e); // 触发消息重试
        }
    }
}